package com.tongzhan.service.impl;

import com.alibaba.fastjson.JSON;
import com.tongzhan.base.PageInfo;
import com.tongzhan.base.Return;
import com.tongzhan.data.vo.*;
import com.tongzhan.entity.User;
import com.tongzhan.entity.religion.*;
import com.tongzhan.repository.UserRepository;
import com.tongzhan.repository.religion.*;
import com.tongzhan.service.InspectionService;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InspectionServiceImpl implements InspectionService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SiteRepository siteRepository;

    @Autowired
    private SiteInfoRepository siteInfoRepository;

    @Autowired
    private InspectionRepository inspectionRepository;

    @Autowired
    private InspectionEvenRepository inspectionEvenRepository;

    @Autowired
    private ProblemTypeRepository typeRepository;

    @Autowired
    private ProblemRepository problemRepository;

    @Autowired
    private RectificationRepository rectificationRepository;

    @Autowired
    private ModuleSignRepository signRepository;

    @Override
    public Return newInspectReport(String userId, Inspection inspection) {
        String inspectionCode = inspection.getInspectionCode();
        //场所信息
        Site site = siteRepository.findByPrimaryKey(inspection.getSiteCode());
        if (site != null) {
            inspection.setStreetName(site.getStreetName());
            inspection.setVillageName(site.getVillageName());
            inspection.setSiteName(site.getSiteName());
            inspection.setReligionCode(site.getReligionCode());
            inspection.setReligionName(site.getReligionName());
        }
        if (StringUtils.isEmpty(inspectionCode)) {
            //新增
            //用户
            User user = userRepository.findByPrimaryKey(userId);
            if (user == null) {
                return new Return(Return.FAIL, "用户不存在");
            }
            inspection.setUserName(user.getUserName());
            inspection.setRealName(user.getRealName());
            //生成电力报告编码
            SnowFlakeUtils snow = new SnowFlakeUtils(4, 8);
            inspection.setInspectionCode(String.valueOf(snow.nextId()));
            //查询电力上报巡查类型
            InspectionEven inspectionEven = inspectionEvenRepository.findOne(new Query().addCriteria(Criteria.where("TypeCode").is(inspection.getTypeCode())));
            if (inspectionEven != null) {
                inspection.setTypeName(inspectionEven.getTypeName());
            }
            //记录去除时间的日期，便于筛选
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            inspection.setTime(LocalDate.now().format(dtf));
            inspection.setCreateTime(LocalDateTime.now());
            inspectionRepository.insert(inspection);
            //查询条件
            Query query = new Query();
            query.addCriteria(Criteria.where("UserId").nin(userId));
            if ("01".equals(inspection.getTypeCode())) {
                //安全模块角标提醒：宗教场所、民宗局、民间信仰点
                List<String> codes = Arrays.asList("01", "04", "07");
                query.addCriteria(Criteria.where("DutiesCode").in(codes));
                List<ModuleSign> signList = signRepository.findAll(query);
                if (!CollectionUtils.isEmpty(signList)) {
                    for (ModuleSign sign : signList) {
                        //安全模块角标加一
                        sign.setSafeCheck(sign.getSafeCheck() + 1);
                        signRepository.updateByPrimaryKey(sign);
                    }
                }
            } else if ("02".equals(inspection.getTypeCode())) {
                //电力模块角标提醒: 宗教场所、第三方、民宗局、民间信仰点
                List<String> codes = Arrays.asList("01", "04", "06", "07");
                query.addCriteria(Criteria.where("DutiesCode").in(codes));
                List<ModuleSign> signList = signRepository.findAll(query);
                if (!CollectionUtils.isEmpty(signList)) {
                    for (ModuleSign sign : signList) {
                        //电力模块角标加一
                        sign.setPowerCheck(sign.getPowerCheck() + 1);
                        signRepository.updateByPrimaryKey(sign);
                    }
                }

            } else if ("03".equals(inspection.getTypeCode())) {
                //消防模块角标提醒：宗教场所、第三方、民宗局、民间信仰点
                List<String> codes = Arrays.asList("01", "04", "06", "07");
                query.addCriteria(Criteria.where("DutiesCode").in(codes));
                List<ModuleSign> signList = signRepository.findAll(query);
                if (!CollectionUtils.isEmpty(signList)) {
                    for (ModuleSign sign : signList) {
                        //消防模块角标加一
                        sign.setFireCheck(sign.getFireCheck() + 1);
                        signRepository.updateByPrimaryKey(sign);
                    }
                }
            }
        } else {
            //修改
            Inspection update = inspectionRepository.findByPrimaryKey(inspectionCode);
            if (update != null) {
                if (update.getState().equals("已整改") && !update.getState().equals(inspection.getState())) {
                    //删除整改记录
                    rectificationRepository.delete(new Query()
                            .addCriteria(Criteria.where("InspectionCode").is(inspectionCode)));
                }
                inspection.setCreateTime(update.getCreateTime());
                inspection.setUserName(update.getUserName());
                inspection.setRealName(update.getRealName());
            }
            inspection.setUpdateTime(LocalDateTime.now());
            inspectionRepository.updateByPrimaryKey(inspection);
        }
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getInspectList(String userId, String timeSlot, String state, Integer type, Integer page, Integer size, String flag) {
        //查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "CreateTime"));
        //用户信息
        User user = userRepository.findByPrimaryKey(userId);
        if (user == null) {
            return new Return(Return.FAIL, "用户不存在");
        }
        //权限编码
        String dutiesCode = user.getDutiesCode();
        if ("02".equals(dutiesCode) || ThreadLocalUtil.STREET_CODE.equals(dutiesCode)) {
            //镇街道
            query.addCriteria(Criteria.where("StreetCode").is(user.getStreetCode()));
        } else if ("03".equals(dutiesCode)) {
            //宗教团体
            query.addCriteria(Criteria.where("ReligionCode").is(user.getReligionCode()));
        } else if ("04".equals(dutiesCode) || "07".equals(dutiesCode)) {
            //场所、民间信仰点
            query.addCriteria(Criteria.where("SiteCode").is(user.getSiteCode()));
        } else if ("05".equals(dutiesCode) || ThreadLocalUtil.VILLAGE_CODE.equals(dutiesCode)) {
            //村社区
            query.addCriteria(Criteria.where("VillageCode").is(user.getVillageCode()));
        }
        if (StringUtils.isNotEmpty(timeSlot)) {
            //现在时间
            LocalDateTime now = LocalDateTime.now();
            //需要查询的时间
            LocalDateTime date;
            switch (timeSlot) {
                case "周":
                    date = now.minusDays(7);
                    break;
                case "月":
                    date = now.minusMonths(1);
                    break;
                case "季":
                    date = now.minusMonths(3);
                    break;
                case "半年":
                    date = now.minusMonths(6);
                    break;
                default:
                    date = now.minusYears(1);
                    break;
            }
            query.addCriteria(Criteria.where("CreateTime").gte(date));
        }
        if (StringUtils.isNotEmpty(state)) {
            switch (state) {
                case "0":
                    query.addCriteria(Criteria.where("State").is("需整改"));
                    break;
                case "1":
                    query.addCriteria(Criteria.where("State").is("已整改"));
                    break;
                case "2":
                    query.addCriteria(Criteria.where("State").is("无问题"));
                    break;
            }
        }
        //小程序端调用
        boolean res = StringUtils.isEmpty(flag);
        //巡查类型
        if (type == 0) {
            //安全检查
            query.addCriteria(Criteria.where("TypeCode").is("01"));
            if (res) {
                //重置安全模块角标
                ModuleSign sign = signRepository.findByPrimaryKey(userId);
                if (sign != null) {
                    sign.setSafeCheck(0L);
                    signRepository.updateByPrimaryKey(sign);
                }
            }
        } else if (type == 1) {
            //电力检查
            query.addCriteria(Criteria.where("TypeCode").is("02"));
            if (res) {
                //重置电力模块角标
                ModuleSign sign = signRepository.findByPrimaryKey(userId);
                if (sign != null) {
                    sign.setPowerCheck(0L);
                    signRepository.updateByPrimaryKey(sign);
                }
            }
        } else if (type == 2) {
            //消防检查
            query.addCriteria(Criteria.where("TypeCode").is("03"));
            if (res) {
                //重置消防模块角标
                ModuleSign sign = signRepository.findByPrimaryKey(userId);
                if (sign != null) {
                    sign.setFireCheck(0L);
                    signRepository.updateByPrimaryKey(sign);
                }
            }
        }
        Page<Inspection> inspections = inspectionRepository.findPage(query, PageRequest.of(page - 1, size));
        //巡查列表
        List<Inspection> inspectionList = inspections.getContent();
        //VO
        List<InspectionVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inspectionList)) {
            for (Inspection inspection : inspectionList) {
                InspectionVO vo = new InspectionVO();
                vo.setInspectionCode(inspection.getInspectionCode());
                vo.setStreetName(inspection.getStreetName());
                vo.setSiteName(inspection.getSiteName());
                vo.setState(inspection.getState());
                vo.setTime(inspection.getCreateTime());
                vo.setStreetCode(inspection.getStreetCode());
                //图片
                String picture = inspection.getPicture();
                //图片列表
                List<PicVO> picList = new ArrayList<>();
                if (StringUtils.isNotEmpty(picture)) {
                    String[] files = picture.split(",");
                    for (String file : files) {
                        PicVO picVO = new PicVO();
                        picVO.setPath(file);
                        picList.add(picVO);
                    }
                }
                vo.setPicList(picList);
                vos.add(vo);
            }
        }
        //分页
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setSize(vos.size());
        pageInfo.setTotal(inspections.getTotalElements());
        //封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("inspectionList", vos);
        map.put("pageInfo", pageInfo);
        return new Return(map);
    }

    @Override
    public Return inspectDetails(String inspectionCode) {
        Inspection inspection = inspectionRepository.findByPrimaryKey(inspectionCode);
        //巡检信息
        InspectionDetailsVO detailsVO = new InspectionDetailsVO();
        //整改信息
        RectificationVO rectificationVO = null;
        if (inspection != null) {
            BeanUtils.copyProperties(inspection, detailsVO);
            //级别
            detailsVO.setReligionName(inspection.getReligionName());
            //巡查状态
            String state = inspection.getState();
            //问题列表
            String checkList = inspection.getCheckList();
            if (StringUtils.isNotEmpty(checkList)) {
                List<ProblemDetailsVO> problemList = JSON.parseArray(checkList, ProblemDetailsVO.class);
                Map<String, Object> map = (Map<String, Object>) this.getProblemList(inspection.getTypeCode()).getData();
                List<ProblemTypeVO> list = (List<ProblemTypeVO>) map.get("typeList");

                if (!CollectionUtils.isEmpty(list)) {
                    List<ProblemTypeVO> selectProblemList = new ArrayList<>();
                    for (ProblemTypeVO item : list) {
                        ProblemTypeVO selectVO = new ProblemTypeVO();
                        BeanUtils.copyProperties(item, selectVO);

                        List<ProblemVO> proList = new ArrayList<>();
                        for (ProblemVO proItem : item.getProblemList()) {
                            ProblemVO proVo = new ProblemVO();
                            BeanUtils.copyProperties(proItem, proVo);

                            List<OpinionVO> opinionList = new ArrayList<>();
                            for (OpinionVO opinionVO : proItem.getOpinionList()) {
                                OpinionVO opinion = new OpinionVO();
                                BeanUtils.copyProperties(opinionVO, opinion);

                                List<ProblemDetailsVO> filterList = problemList.stream().filter(it -> it.getProblemCode().equals(proItem.getProblemCode())).collect(Collectors.toList());
                                opinion.setIsSelected(Boolean.FALSE);
                                if (!CollectionUtils.isEmpty(filterList)) {
                                    ProblemDetailsVO filter = filterList.get(0);
                                    opinion.setIsSelected(filter.getOption().equals(opinionVO.getOpinion()));
                                }
                                opinionList.add(opinion);
                            }
                            proVo.setOpinionList(opinionList);
                            proList.add(proVo);
                        }
                        selectVO.setProblemList(proList);
                        selectProblemList.add(selectVO);
                    }
                    detailsVO.setSelectProblemList(selectProblemList);
                }

                if (!CollectionUtils.isEmpty(problemList)) {
                    for (ProblemDetailsVO problemDetailsVO : problemList) {
                        Problem problem = problemRepository.findByPrimaryKey(problemDetailsVO.getProblemCode());
                        if (problem != null) {
                            problemDetailsVO.setProblemName(problem.getProblemName());
                        }
                    }
                }
                detailsVO.setProblemList(problemList);
            } else {
                detailsVO.setProblemList(new ArrayList<>());
            }

            if ("无问题".equals(state)) {
                detailsVO.setTitle("无问题");
            } else {
                if ("01".equals(inspection.getTypeCode())) {
                    //安全隐患
                    detailsVO.setTitle("安全隐患");
                } else if ("02".equals(inspection.getTypeCode())) {
                    //电力隐患
                    detailsVO.setTitle("电力隐患");
                } else if ("03".equals(inspection.getTypeCode())) {
                    //消防隐患
                    detailsVO.setTitle("消防隐患");
                }
            }
            if ("已整改".equals(state)) {
                rectificationVO = new RectificationVO();
                //查询对应整改信息
                Rectification rectification
                        = rectificationRepository.findOne(new Query().addCriteria(Criteria.where("InspectionCode").is(inspection.getInspectionCode())));
                if (rectification != null) {
                    rectificationVO.setRectifiers(rectification.getRectificationPersonnel());
                    rectificationVO.setDescribe(rectification.getDescribe());
                    rectificationVO.setRectificationTime(rectification.getRectificationTime());
                    //图片
                    String picture = rectification.getPicture();
                    List<PicVO> picList = new ArrayList<>();
                    if (StringUtils.isNotEmpty(picture)) {
                        String[] paths = picture.split(",");
                        for (String path : paths) {
                            PicVO picVO = new PicVO();
                            picVO.setPath(path);
                            picList.add(picVO);
                        }
                    }
                    rectificationVO.setPicList(picList);
                }
            }
            detailsVO.setSign(inspection.getSign());
            detailsVO.setSiteName(inspection.getSiteName());
            //类型
            InspectionEven inspectionEven = inspectionEvenRepository.findByPrimaryKey(inspection.getTypeCode());
            if (inspectionEven != null) {
                detailsVO.setTypeName(inspectionEven.getTypeName());
            }
            detailsVO.setReportPersonnel(inspection.getUserName());
            detailsVO.setDescribe(inspection.getDescribe());
            detailsVO.setTime(inspection.getCreateTime());
            //图片
            String picture = inspection.getPicture();
            List<PicVO> picList = new ArrayList<>();
            if (StringUtils.isNotEmpty(picture)) {
                String[] paths = picture.split(",");
                for (String path : paths) {
                    PicVO picVO = new PicVO();
                    picVO.setPath(path);
                    picList.add(picVO);
                }
            }
            detailsVO.setPicList(picList);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("beforeRectification", detailsVO);
        map.put("afterRectification", rectificationVO);
        return new Return(map);
    }

    @Override
    public Return editProblemType(String typeCode, String problemTypeCode, String problemTypeName) {
        //巡查类型
        InspectionEven even = inspectionEvenRepository.findByPrimaryKey(typeCode);
        if (StringUtils.isEmpty(problemTypeCode)) {
            //新增
            ProblemType type = new ProblemType();
            SnowFlakeUtils snow = new SnowFlakeUtils(4, 13);
            type.setProblemTypeCode(String.valueOf(snow.nextId()));
            if (even != null) {
                type.setTypeCode(even.getTypeCode());
                type.setTypeName(even.getTypeName());
            }
            type.setProblemTypeName(problemTypeName);
            type.setCreateTime(LocalDateTime.now());
            typeRepository.insert(type);
        } else {
            //编辑
            ProblemType problemType = typeRepository.findByPrimaryKey(problemTypeCode);
            if (problemType != null) {
                if (even != null) {
                    problemType.setTypeCode(even.getTypeCode());
                    problemType.setTypeName(even.getTypeName());
                }
                problemType.setProblemTypeName(problemTypeName);
                problemType.setUpdateTime(LocalDateTime.now());
                typeRepository.updateByPrimaryKey(problemType);
            }
        }
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getProblemTypeList(String typeCode) {
        //VO
        List<ProblemTypeVO> vos = new ArrayList<>();
        //问题类型列表
        List<ProblemType> typeList = typeRepository.findAll(new Query().addCriteria(Criteria.where("TypeCode").is(typeCode)));
        if (!CollectionUtils.isEmpty(typeList)) {
            for (ProblemType type : typeList) {
                ProblemTypeVO vo = new ProblemTypeVO();
                vo.setProblemTypeCode(type.getProblemTypeCode());
                vo.setProblemTypeName(type.getProblemTypeName());
                vos.add(vo);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("problemTypeList", vos);
        return new Return(map);
    }

    @Override
    public Return deleteProblemType(String problemTypeCode) {
        //删除问题类型
        typeRepository.delete(new Query().addCriteria(Criteria.where("ProblemTypeCode").is(problemTypeCode)));
        //删除对应问题
        problemRepository.delete(new Query().addCriteria(Criteria.where("ProblemTypeCode").is(problemTypeCode)));
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getProblemList(String typeCode) {
        //问题类型列表
        List<ProblemType> problemTypeList = typeRepository.findAll(new Query().addCriteria(Criteria.where("TypeCode").is(typeCode)));
        //返回列表
        List<ProblemTypeVO> problemTypeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(problemTypeList)) {
            for (ProblemType type : problemTypeList) {
                //ProblemTypeVO
                ProblemTypeVO problemTypeVO = new ProblemTypeVO();
                problemTypeVO.setProblemTypeCode(type.getProblemTypeCode());
                problemTypeVO.setProblemTypeName(type.getProblemTypeName());
                //返回列表
                List<ProblemVO> problemVOS = new ArrayList<>();
                //问题列表
                Query query = new Query();
                query.with(Sort.by(Sort.Direction.ASC, "CreateTime"));
                List<Problem> problemList = problemRepository.findAll(query.addCriteria(Criteria.where("ProblemTypeCode").is(type.getProblemTypeCode())));
                if (!CollectionUtils.isEmpty(problemList)) {
                    for (Problem problem : problemList) {
                        //ProblemVO
                        ProblemVO vo = new ProblemVO();
                        vo.setProblemCode(problem.getProblemCode());
                        vo.setProblemName(problem.getProblemName());
                        //选项
                        String option = problem.getOption();
                        List<OpinionVO> opinionList = new ArrayList<>();
                        if (StringUtils.isNotEmpty(option)) {
                            String[] split = option.split(",");
                            for (String o : split) {
                                OpinionVO opinionVO = new OpinionVO();
                                opinionVO.setOpinion(o);
                                opinionList.add(opinionVO);
                            }
                        }
                        vo.setOpinionList(opinionList);
                        problemVOS.add(vo);
                    }
                }
                problemTypeVO.setProblemList(problemVOS);
                problemTypeVOS.add(problemTypeVO);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("typeList", problemTypeVOS);
        return new Return(map);
    }

    @Override
    public Return editProblem(String problemCode, String problemTypeCode, String problemName, String opinion) {
        if (StringUtils.isEmpty(problemCode)) {
            //新增
            Problem problem = new Problem();
            SnowFlakeUtils snow = new SnowFlakeUtils(4, 14);
            problem.setProblemCode(String.valueOf(snow.nextId()));
            problem.setProblemName(problemName);
            //问题类型
            ProblemType type = typeRepository.findByPrimaryKey(problemTypeCode);
            if (type != null) {
                problem.setProblemTypeCode(type.getProblemTypeCode());
                problem.setProblemTypeName(type.getProblemTypeName());
            }
            problem.setOption(opinion);
            problem.setCreateTime(LocalDateTime.now());
            problemRepository.insert(problem);
        } else {
            //修改
            Problem problem = problemRepository.findByPrimaryKey(problemCode);
            if (problem != null) {
                problem.setProblemName(problemName);
                problem.setOption(opinion);
                problem.setUpdateTime(LocalDateTime.now());
                problemRepository.updateByPrimaryKey(problem);
            }
        }
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return deleteProblem(String problemCode) {
        problemRepository.delete(new Query().addCriteria(Criteria.where("ProblemCode").is(problemCode)));
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getTypeList() {
        List<InspectionEven> evens = inspectionEvenRepository.findAll();
        //VO
        List<TypeVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(evens)) {
            for (InspectionEven even : evens) {
                TypeVO vo = new TypeVO();
                vo.setTypeCode(even.getTypeCode());
                vo.setTypeName(even.getTypeName());
                vos.add(vo);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("typeList", vos);
        return new Return(map);
    }

    @Override
    public Return rectification(Rectification rectification) {
        //整改编码
        SnowFlakeUtils snow = new SnowFlakeUtils(4, 15);
        rectification.setRectificationCode(String.valueOf(snow.nextId()));
        //巡查信息
        Inspection inspection = inspectionRepository.findByPrimaryKey(rectification.getInspectionCode());
        if (inspection == null) {
            return new Return(Return.FAIL, "整改失败");
        }
        if ("已整改".equals(inspection.getState()) || "无问题".equals(inspection.getState())) {
            return new Return(Return.FAIL, "无需整改");
        }
        //整改
        inspection.setState("已整改");
        inspection.setUpdateTime(LocalDateTime.now());
        inspectionRepository.updateByPrimaryKey(inspection);
        rectification.setRectificationCompany(inspection.getSiteName());
        rectification.setRectificationTime(LocalDateTime.now());
        rectificationRepository.insert(rectification);
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return selectNotReported(String time, Integer week, Integer type) {
        //返回数据
        Map<String, Object> map = new HashMap<>();
        //查询条件
        Query query = new Query();
        //匹配日期
        query.addCriteria(Criteria.where("Time").regex(".*?" + time + ".*"));
        //巡查类型
        if (type == 0) {
            //安全检查
            query.addCriteria(Criteria.where("TypeCode").is("01"));
        } else if (type == 1) {
            //电力检查
            query.addCriteria(Criteria.where("TypeCode").is("02"));
        } else if (type == 2) {
            //消防检查
            query.addCriteria(Criteria.where("TypeCode").is("03"));
        }
        // 获取该日期的当月第一个星期一的日期
        LocalDateTime date = DateSwitcher.getFirstMondayOfTheMonth(time);
        if (date == null) {
            return new Return(Return.FAIL, "日期格式不正确");
        }
        //查询开始日期
        LocalDateTime start = date.with(TemporalAdjusters.firstDayOfMonth());
        //查询结束日期
        LocalDateTime end = date.with(TemporalAdjusters.lastDayOfMonth());
        //匹配周数
        if (week == 0) {
            //此周属于上月
            // 当月的第几天
            int dayOfMonth = date.getDayOfMonth();
            // 和当月第一天的差值
            int i = dayOfMonth - 1;
            if (i == 0) {
                map.put("siteList", new ArrayList<>());
                return new Return(map);
            } else {
                try {
                    start = date.with(TemporalAdjusters.firstDayOfMonth());
                } catch (Exception e) {
                    return new Return(Return.FAIL, "日期格式不正确");
                }
                end = start.plusDays(i - 1);
            }
        } else if (week == 1) {
            //第一周
            start = DateSwitcher.getMinTime(date);
            end = start.plusDays(6);
        } else if (week == 2) {
            //第二周
            start = DateSwitcher.getMinTime(date.plusDays(7));
            end = start.plusDays(6);
        } else if (week == 3) {
            //第三周
            start = date.plusDays(14);
            end = start.plusDays(6);
        } else if (week == 4) {
            //第四周
            start = date.plusDays(21);
            //判断end是否到下个月份
            if (date.getMonthValue() == end.getMonthValue()) {
                end = start.plusDays(6);
            }
        }
        //start当天最小时间
        start = DateSwitcher.getMinTime(start);
        end = DateSwitcher.getMaxTime(end);
        query.addCriteria(Criteria.where("CreateTime").gte(start).andOperator(Criteria.where("CreateTime").lte(end)));
        //巡查列表
        List<Inspection> inspectionList = inspectionRepository.findAll(query);
        //封装VO
        List<SiteVO> siteVOS = new ArrayList<>();
        //场所名称列表
        List<String> codes = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inspectionList)) {
            for (Inspection inspection : inspectionList) {
                String siteCode = inspection.getSiteCode();
                if (!codes.contains(siteCode)) {
                    codes.add(siteCode);
                }
            }
        }
        List<Site> siteList = siteRepository.findAll(new Query().addCriteria(Criteria.where("SiteCode").nin(codes)));
        if (!CollectionUtils.isEmpty(siteList)) {
            for (Site site : siteList) {
                SiteVO siteVO = new SiteVO();
                siteVO.setSiteCode(site.getSiteCode());
                siteVO.setSiteName(site.getSiteName());
                //场所信息
                SiteInfo siteInfo = siteInfoRepository.findOne(new Query().addCriteria(Criteria.where("SiteCode").is(site.getSiteCode())));
                if (siteInfo != null) {
                    siteVO.setSiteTelephone(siteInfo.getPersonTelephone());
                }
                siteVOS.add(siteVO);
            }
        }
        map.put("siteList", siteVOS);
        return new Return(map);
    }

    @Override
    public Return delete(String inspectionCode) {
        inspectionRepository.deletePrimaryKey(inspectionCode);
        return new Return(Return.SUCCESS, "删除成功");
    }
}
